package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.PraiseNotice_RVAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.dongtai.DynamicRelayDetailActivity;
import com.beiing.tianshuai.tianshuai.dongtai.view.DynamicDetailActivity;
import com.beiing.tianshuai.tianshuai.entity.ConsumeMarksBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseNoticeBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.mine.ui.NewPersonalIndexActivity;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;
import com.beiing.tianshuai.tianshuai.widget.MyRefreshHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by magikliu on 2017/9/27.
 */

public class PraiseNoticeActivity extends BaseActivity {

    private PraiseNotice_RVAdapter mAdapter;
    private List<DynamicPraiseNoticeBean.DataBean.NumberBean> mNoticeList;      //容器数据源
    private List<DynamicPraiseNoticeBean.DataBean.NumberBean> numberBeanList;   //数据中转
    private String uid;
    private int firstRow = 0;
    private int listRows = 10;
    private boolean isLoadFinish;       //是否已经加载全部

    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.rcv_praise)
    RecyclerView mRecyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.refresh_header)
    MyRefreshHeader mRefreshHeader;
    @BindView(R.id.refresh_footer)
    ClassicsFooter mRefreshFooter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_praise_notice;
    }

    @Override
    protected void init() {
        uid = UserInfoBean.getUid(mContext);

        ivToolbarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initRecyclerView();
        initListener();
        loadData(uid, firstRow, listRows);
        consumeMarks(uid);
    }

    private void initListener() {
        mRefreshLayout.setRefreshHeader(new MyRefreshHeader(mContext));
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (!isLoadFinish){
                    loadData(uid, firstRow, listRows);
                } else {
                    showShortText("已加载全部内容");
                    mRefreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mNoticeList.clear();
                firstRow = 0;
                loadData(uid, firstRow, listRows);
                mRefreshHeader.onFinish(mRefreshLayout, true);
            }
        });
    }

    private void initRecyclerView() {
        mNoticeList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new PraiseNotice_RVAdapter(mContext, mNoticeList);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemViewClickListener(new PraiseNotice_RVAdapter.OnItemViewClickListener() {
            @Override
            public void onHeaderClick(View view, int position, DynamicPraiseNoticeBean.DataBean.NumberBean bean) {
                Intent intent = new Intent(mContext, NewPersonalIndexActivity.class);
                intent.putExtra("uid", bean.getUid());
                startActivity(intent);
            }

            @Override
            public void onReplyClick(View view, int position, DynamicPraiseNoticeBean.DataBean.NumberBean bean) {
                if (null == bean.getContent()){
                    showShortText("此动态不存在！");
                } else {
                    Intent intent = new Intent();
                    switch (bean.getTable_type()){
                        case "0":
                            intent.setClass(mContext, DynamicDetailActivity.class);
                            break;
                        case "1":
                            intent.setClass(mContext, DynamicDetailActivity.class);
                            break;
                        case "2":
                            intent.setClass(mContext, DynamicRelayDetailActivity.class);
                            break;
                        case "3":
                            intent.setClass(mContext, DynamicRelayDetailActivity.class);
                            break;
                    }
                    intent.putExtra("dynamic_id", bean.getToId());
                    startActivity(intent);
                }
            }

            @Override
            public void onContentClick(View view, int position, DynamicPraiseNoticeBean.DataBean.NumberBean bean) {
                if (null == bean.getContent()){
                    showShortText("此动态不存在！");
                } else {
                    Intent intent = new Intent();
                    switch (bean.getTable_type()){
                        case "0":
                            intent.setClass(mContext, DynamicDetailActivity.class);
                            break;
                        case "1":
                            intent.setClass(mContext, DynamicDetailActivity.class);
                            break;
                        case "2":
                            intent.setClass(mContext, DynamicRelayDetailActivity.class);
                            break;
                        case "3":
                            intent.setClass(mContext, DynamicRelayDetailActivity.class);
                            break;
                    }
                    intent.putExtra("dynamic_id", bean.getToId());
                    startActivity(intent);
                }
            }
        });
    }

    private void showNoticeList(List<DynamicPraiseNoticeBean.DataBean.NumberBean> numberBeanList){
        int size = numberBeanList.size();
        if (size == 0){
            isLoadFinish = true;
        } else if (size < listRows){
            isLoadFinish = true;
            firstRow += size;
        } else {
            isLoadFinish = false;
            firstRow += size;
        }
        mNoticeList.addAll(numberBeanList);
        mAdapter.notifyDataSetChanged();
        if (mRefreshLayout != null && (mRefreshLayout.isRefreshing() || mRefreshLayout.isLoading())){
            mRefreshLayout.finishRefresh();
            mRefreshLayout.finishLoadmore();
        }
    }

    private void loadData(String uid, int firstRow, int listRows) {
        showProgress();
        DisposableObserver<DynamicPraiseNoticeBean> observer = new DisposableObserver<DynamicPraiseNoticeBean>() {
            @Override
            public void onNext(@NonNull DynamicPraiseNoticeBean dynamicPraiseNoticeBean) {
                hideProgress();
                numberBeanList = dynamicPraiseNoticeBean.getData().getNumber();
                showNoticeList(numberBeanList);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                showShortText("数据获取错误，请稍后重试");
            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getDynamicPraiseNoticeResult(uid, firstRow, listRows)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    private void consumeMarks(String uid) {
        DisposableObserver<ConsumeMarksBean> observer = new DisposableObserver<ConsumeMarksBean>() {
            @Override
            public void onNext(@NonNull ConsumeMarksBean consumeMarksBean) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getConsumeThingMarksResult(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    private void showProgress(){
        CustomProgressDialog.showLoading(mContext, "请稍后，正在努力加载...", true);
    }

    private void hideProgress(){
        CustomProgressDialog.stopLoading();
    }
}
